package com.abel.bigwater.dataserver.model.ewide

import com.abel.bigwater.data.BcdString
import com.alibaba.fastjson.annotation.JSONField
import io.netty.buffer.ByteBuf
import java.util.*

class KmingUploadReqData(_pduLen: Int = -1) : TermData(KmingDataType.TermUploadReply, _pduLen) {
    var backTimeStr: BcdString? = null

    var backTime: Date?
        get() = backTimeStr?.toFullYearTime()
        set(value) {
            backTimeStr = if (value == null) null else fromFullYearTime(value)
        }

    var respondType: SessionReponseType? = null

    override fun bufLength() = 1 + 7 + 1

    override fun parseBuf(buf: ByteBuf): Boolean {
        buf.apply {
            assert(type.code == readUnsignedByte())

            backTimeStr = BcdString.create(this, 7)

            respondType = SessionReponseType.findByCode(readByte())
        }
        return true
    }

    override fun pack(buf: ByteBuf) {
        buf.alloc().buffer().apply {
            writeByte(type.code.toInt())

            backTime
            writeBytes(backTimeStr?.hexArray)

            writeByte(respondType!!.code.toInt())

            // 整包写入
            buf.writeBytes(this)
        }
    }
}